package com.eorchis.module.signupdept.ui.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.eorchis.core.service.IBaseService;
import com.eorchis.core.servicetemplate.treetemplate.jsonbean.JsonTreeBean;
import com.eorchis.core.ui.controller.AbstractBaseController;
import com.eorchis.module.department.domain.DepartmentTreeCondition;
import com.eorchis.module.department.service.IDepartmentTreeService;
import com.eorchis.module.signupdept.service.ISignupDeptService;
import com.eorchis.module.signupdept.service.ISignupDeptTreeService;
import com.eorchis.module.signupdept.ui.commond.SignupDeptQueryCommond;
import com.eorchis.module.signupdept.ui.commond.SignupDeptValidCommond;
import com.eorchis.utils.utils.PropertyUtil;

/**
 * Title: SignupDeptController<br>
 * Description: 报名单位管理控制器<br>
 * Company: GoldGov<br>
 * Copyright @ 2012 GoldGov .All rights reserved.<br>
 * @author Max
 * @createDate 2014-12-29
 * @version $Revision$
 */
@Controller("signupDeptController")
@RequestMapping(value = SignupDeptController.MODULE_PATH)
public class SignupDeptController 
		extends AbstractBaseController<SignupDeptValidCommond,SignupDeptQueryCommond> 
{
	// 模块页面根路径
	public static final String MODULE_PATH = "/module/signupdept";
	// 报名单位接口
	@Autowired
	@Qualifier("com.eorchis.module.signupdept.service.impl.SignupDeptServiceImpl")
	private ISignupDeptService signupDeptService;
	
	@Autowired
	@Qualifier("com.eorchis.module.signupdept.service.impl.SignupDeptTreeServiceImpl")
	private ISignupDeptTreeService signupDeptTreeService;
	
	public IBaseService getService() {
		return signupDeptService;
	}

	public String getModulePath() {
		return MODULE_PATH;
	}
	
	/**
	 * 报名单位树的查询
	 * @param queryCommond
	 * @param request
	 * @param response
	 * @param resultState
	 * @throws Exception
	 * @author Max
	 * @createDate 2014-12-30
	 */
	@RequestMapping(value = "/getSignupDeptTree")
	public @ResponseBody
	List<JsonTreeBean> getSignupDeptTree(SignupDeptQueryCommond commond,HttpServletRequest request) throws Exception {
		List<JsonTreeBean> result = new ArrayList<JsonTreeBean>();
		//取出当前管理范围
		if(!PropertyUtil.objectNotEmpty(commond.getNode())||commond.getNode().equals(JsonTreeBean.ROOT_NODE_PARENT_ID)){
			commond.setNode("1");
			DepartmentTreeCondition condition = new DepartmentTreeCondition();
			BeanUtils.copyProperties(commond, condition);
			condition.setFindLeapNode(true);
			List<JsonTreeBean> list = signupDeptTreeService.doProcess(condition);
			JsonTreeBean rootBean = new JsonTreeBean();
			rootBean.setText("报名单位");
			rootBean.setLeaf(false);
			rootBean.setId("1");
			rootBean.setChildren(list);
			rootBean.setState("close");
			rootBean.setExpandable(true);
			result.add(rootBean);
		} else {
			DepartmentTreeCondition condition = new DepartmentTreeCondition();
			BeanUtils.copyProperties(commond, condition);
			condition.setFindLeapNode(true);
			result = signupDeptTreeService
					.doProcess(condition);
		}
		return result;
	}
}
